package com.ying.controller.order;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ApiResponse;
import com.ying.po.order.Seller;
import com.ying.service.order.SellerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/order/seller")
@Api(tags = "Seller接口")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @GetMapping("/{id}")
    @SaCheckPermission("seller-get")
    @ApiOperation("根据ID查询seller")
    public ApiResponse finById(@PathVariable(value = "id") Integer id) {
        return ApiResponse.success(sellerService.findById(id));
    }

    @PostMapping("/create")
    @SaCheckPermission("seller-create")
    @ApiOperation("创建seller对象")
    public ApiResponse create(@RequestBody Seller seller) {
        // 存储seller
        Integer id = sellerService.create(seller);
        return ApiResponse.success(id);
    }

    @PostMapping("/page")
    @ApiOperation("分页查询")
    @SaCheckPermission("seller-get")
    public ApiResponse findPage(@RequestBody PageWrap<Seller> pageWrap) {
        Integer userId = pageWrap.getModel().getUserId();
        return ApiResponse.success(sellerService.findAllByPage(pageWrap, userId));
    }

    @PutMapping("/update")
    @ApiOperation("修改Seller")
    @SaCheckPermission("seller-update")
    public ApiResponse updateById(@RequestBody Seller seller) {
        sellerService.updateById(seller);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除")
    @SaCheckPermission("seller-delete")
    public ApiResponse delete(@PathVariable Integer id) {
        sellerService.deleteById(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/delete")
    @ApiOperation("根据ID列表批量删除")
    @SaCheckPermission("seller-delete")
    public ApiResponse deleteByIds(@RequestBody List<Integer> ids) {
        sellerService.deleteByIdInBatch(ids);
        return ApiResponse.success(null);
    }

    @GetMapping("/countall")
    @ApiOperation("查询数量")
    @SaCheckPermission("seller-get")
    public ApiResponse countAll(@RequestParam("userId") Integer userId) {
        long count = sellerService.countAll(userId);
        return ApiResponse.success(count);
    }

    @PostMapping("/findbyids")
    @ApiOperation("根据ID列表查询")
    @SaCheckPermission("seller-get")
    public ApiResponse findByIds(@RequestBody Integer[] ids) {
        List<Seller> userList = sellerService.findByIdInBatch(Arrays.asList(ids));
        return ApiResponse.success(userList);
    }

    @GetMapping("/find/all")
    @ApiOperation("查询全部")
    @SaCheckPermission("seller-get")
    public ApiResponse findAll(@RequestParam("userId") Integer userId) {
        ApiResponse success = ApiResponse.success(sellerService.findAll(userId));
        return success;
    }

    @PostMapping("/query")
    @ApiOperation("根据条件查询Seller")
    @SaCheckPermission("seller-get")
    public ApiResponse queryAllByPage(@RequestBody PageWrap<Seller> pageWrap) {
        return ApiResponse.success(sellerService.queryAllByPage(pageWrap));
    }
//
//    @GetMapping("/name/all")
//    @ApiOperation("查询全部的seller name")
//    @SaCheckPermission("seller-get")
//    public ApiResponse getAllSellerName(@RequestParam("userId") Integer userId) {
//        return ApiResponse.success(sellerService.getAllBuyerName(userId));
//    }

    @GetMapping("/province/exist")
    @ApiOperation("查询当前系统用户所有的省份")
    @SaCheckPermission("seller-get")
    public ApiResponse queryProvince(@RequestParam("userId")Integer userId ) {
        return ApiResponse.success(sellerService.queryProvince(userId));
    }

    @GetMapping("/count/buyer")
    @ApiOperation("查询买家数量")
    @SaCheckPermission("seller-get")
    public ApiResponse getBuyerCount(@RequestParam("userId")Integer userId ) {
        return ApiResponse.success(sellerService.getBuyerCount(userId));
    }



}
